﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

        //     CrystalReportViewer1.ReportSource =
        //this.MapPath("WesternSales.rpt");
        //     CrystalReportViewer2.ReportSource =
        //     this.MapPath("EasternSales.rpt");
        //     // Create the event handler. The ReportRefresh event for 
        //     // CrystalReportViewer2 will be handled by the OnRefresh 
        //     // method when the report in CrystalReportViewer1 is refreshed.
        //     this.CrystalReportViewer1.ReportRefresh += new
        //     CrystalDecisions.Web.RefreshEventHandler
        //     (CrystalReportViewer2.OnRefresh);


        //try
        //{
        //    ConnectionInfo connInfo = new ConnectionInfo();
        //    connInfo.ServerName = "Kronos";
        //    connInfo.DatabaseName = "MCSIG";
        //    connInfo.UserID = "sa";
        //    connInfo.Password = "a=bxh/2";
        //    int i = 0;
        //    foreach (TableLogOnInfo obj in CrystalReportViewer1.LogOnInfo)
        //    {
        //        obj.ConnectionInfo = connInfo;
        //        i++;
        //    }

        //    lblMensaje.Text = i.ToString();
        //}
        //catch (Exception ex)
        //{
        //    lblMensaje.Text = lblMensaje.Text + " " + ex.Message.ToString();
        //}


        try
        {
           




            //if (!IsPostBack)
            //{
            
            //if (Session["rep"] == null)
            //{

                ConnectionInfo connInfo = new ConnectionInfo();
                connInfo.ServerName = "Kronos";
                connInfo.DatabaseName = "MCSIG";
                connInfo.UserID = "sa";
                connInfo.Password = "a=bxh/2";
                

                ReportDocument rep = new ReportDocument();
                //rep.SetDatabaseLogon("sa","a=bxh/2","172.17.0.9","MCSIG");
                rep.Load(Server.MapPath("IvaGeneral.rpt"),OpenReportMethod.OpenReportByTempCopy);
                CrystalReportViewer1.ReportSource = rep;

                Tables repTbls = rep.Database.Tables;

                foreach (CrystalDecisions.CrystalReports.Engine.Table repTbl in repTbls)
                {
                    TableLogOnInfo repTblLogonInfo = repTbl.LogOnInfo;
                    repTblLogonInfo.ConnectionInfo = connInfo;
                    repTbl.ApplyLogOnInfo(repTblLogonInfo);
                    repTbl.Location = "MCSIG" + ".dbo." + repTbl.Name; 
                    //i++;
                    
                }
                rep.Refresh();
                //Session["rep"] = rep;
                //lblMensaje.Text = i.ToString();
            //}

            CrystalReportViewer1.ReportSource = rep;
            CrystalReportViewer1.DataBind();

            


        }
        catch (Exception ex)
        {
            lblMensaje.Text = lblMensaje.Text + " " + ex.Message;
        }


    }
    protected void btnMostrarReporte_Click(object sender, EventArgs e)
    {
        CrystalReportViewer1.Visible = true;
        CrystalReportViewer1.ReportSource = Session["rep"];
        CrystalReportViewer1.DataBind();
    }

}
